package 笔试;

import javax.swing.*;
import java.util.Arrays;

/**
 * @author:谢君臣
 * @Date:2021/9/18-15:27
 * @version:1.0
 * @function:
 */
public class Main02 {
    public int maxProfit (int k, int[] prices, int fee) {
        // write code here
        if (prices.length==0){return 0;}
        int n = prices.length;
        k = Math.min(k,n/2);
        int[][] buy = new int[n][k+1];
        int[][] sell = new int[n][k+1];
        buy[0][0] = -prices[0];
        sell[0][0]=0;
        for (int i = 0; i <= k ; i++) {
            buy[0][i] = sell[0][i] = Integer.MIN_VALUE/2;
        }
        int i=1,j=1;
        while (i<n)
        {
            buy[i][0] = Math.max(buy[i-1][0],sell[i-1][0]-prices[i]);
            while (j<=k)
            {
                buy[i][0] = Math.max(buy[i-1][j],sell[i-1][j]-prices[i]);
                buy[i][0] = Math.max(buy[i-1][j],sell[i-1][j-1]+prices[i]);
                j++;
            }
            i++;
        }
        return Arrays.stream(sell[n-1]).max().getAsInt();
    }
    public String multiply (String num1, String num2) {
        // write code here
        char[] chars1 = num1.toCharArray();
        char[] chars2 = num1.toCharArray();
        if ((chars1.length==1&&chars1[0]=='0')||(chars2.length==1&&chars2[0]=='0')){
            return "0";
        }int[] nums = new int[chars1.length+chars2.length];
        int index=0;
        for (int i = chars2.length-1; i >=0 ; i--) {
            index=nums.length-(chars2.length-i);
            int c2 = chars2[i]-'0';
            int k=0;
            for (int j = chars1.length-1; j >=0 ; j--) {
                int c1 = chars1[j]-'0';
                int mul = c1*c2;
                int sum = nums[index]+mul+k;
                nums[index--]=sum%10;
                k=sum/10;
            }
            nums[index]=k;
        }
        if (nums[index]==0)index++;
        StringBuilder res = new StringBuilder();
        for (; index <nums.length ; index++) {
            res.append(nums[index]);
        }
        return res.toString();
    }
}
